package com.meixiang.activity.homes.cosmeticTraining;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.myAppointment.AppointmentSaleActivity;
import com.meixiang.entity.training.TrainingAppointmentDetails;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 参加美容培训课程详情
 */
public class CosmeticTrainingCourseDetailActivity extends BaseActivity {

    @Bind(R.id.title)
    TitleView titleView;
    @Bind(R.id.training_course_tv_course_name)
    TextView tvCourseName;
    @Bind(R.id.training_course_tv_course_state)
    TextView tvCourseState;
    @Bind(R.id.training_course_tv_service)
    TextView tvService;
//    @Bind(R.id.training_course_tv_tel)
//    TextView tvTel;
//    @Bind(R.id.training_course_tv_reimburse)
//    TextView tvReimburse;
    @Bind(R.id.rl_serve_pass)
    RelativeLayout rlServePass;
    @Bind(R.id.training_detail_tv_merchant_information)
    TextView tvMerchantInformation;
    @Bind(R.id.training_course_tv_merchant_content)
    TextView tvMerchantContent;
    @Bind(R.id.training_detail_iv_tel)
    ImageView ivTel;
    @Bind(R.id.training_detail_relative_merchant_information)
    RelativeLayout relativeMerchantInformation;
    @Bind(R.id.training_detail_tv_serve_time)
    TextView tTvServeTime;
    @Bind(R.id.training_course_tv_leave_message)
    TextView tvLeaveMessage;
    @Bind(R.id.ll_comment)
    LinearLayout llComment;
    @Bind(R.id.training_course_tv_total_price)
    TextView tvTotalPrice;
    @Bind(R.id.training_course_tv_coupon)
    TextView tvCoupon;
    @Bind(R.id.order_detail_tv_disbursements)
    TextView tvDisbursements;
    @Bind(R.id.training_course_tv_payment)
    TextView tvPayment;
    @Bind(R.id.training_course_tv_payment_confirm_time)
    TextView tvPaymentConfirmTime;
    @Bind(R.id.all_order_item_iv)
    ImageView ivShopping;
    @Bind(R.id.all_order_item_tv_detail)
    TextView tvDetail;
    @Bind(R.id.all_order_item_tv_price)
    TextView tvPrice;
    @Bind(R.id.all_order_item_tv_amount)
    TextView tvAmount;
    @Bind(R.id.training_course_tv_order_no)
    TextView tvOrderNo;
    @Bind(R.id.training_course_detail_recycler)
    RecyclerView recyclerView;
    private String orderId,tel;
    private ServicePasswordAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_training_course_detail);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
//        titleView.setTitle(R.string.appointment_detial_text);
        titleView.setTitle("预约详情");
        titleView.setLeftImageButton(R.mipmap.back);
        orderId = getIntent().getStringExtra("orderId");
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        getTrainingDetail();
    }

    @Override
    protected void initView() {
        mAdapter=new ServicePasswordAdapter();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(mAdapter);
//        for(int i=0;i<2;i++){
//            OrderLinearLayout orderLinearLayout;
//
//            if(i==2-1){
//                orderLinearLayout=new OrderLinearLayout(context,false);
//            }else{
//                orderLinearLayout=new OrderLinearLayout(context,true);
//            }
//            trainingCourseLinearOrder.addView(orderLinearLayout);
//        }

    }
    //R.id.training_course_tv_reimburse
    @OnClick({R.id.training_detail_iv_tel})
    void click(View view){
//        if(view.equals(tvReimburse)){
//            Intent intent=new Intent();
//            intent.setClass(context,AppointmentSaleActivity.class);
//            intent.putExtra("orderId",orderId);
//            startActivity(intent);
//        }else
        if(view.equals(ivTel)){
            if(!AbStrUtil.isEmpty(tel)){
                Intent intentPhone = new Intent(Intent.ACTION_DIAL);
                intentPhone.setData(Uri.parse("tel:" + tel));
                if (intentPhone.resolveActivity(getPackageManager()) != null) {
                    startActivity(intentPhone);
                }
            }
        }

    }


    private void getTrainingDetail() {
        String url = Config.TRAINING_FOR_ORDER_DETAIL;
        HttpParams params = new HttpParams();
        if (!AbStrUtil.isEmpty(orderId)) {
            params.put("orderId", orderId);
        }
        HttpUtils.post(url, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if ("0".equals(code)) {
                    TrainingAppointmentDetails details = AbJsonUtil.fromJson(result.toString(), TrainingAppointmentDetails.class);
                    tel=details.getStoreTel();
                    if(details.getServicePwds()!=null&&details.getServicePwds().size()>0){
                        mAdapter.addAll(details.getServicePwds());
                    }
//                    String servicePwd = result.optString("servicePwd");


                    setDetailData(details);
                } else {
                    Tool.showTextToast(context, message);
                }

            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(context, message);

            }
        });
    }

    private void setDetailData(TrainingAppointmentDetails details) {
        tvCourseName.setText(details.getStoreName());
        tvDetail.setText(details.getGoodsName());
        //预约状态
        if(!AbStrUtil.isEmpty(details.getOrderState())){
            //0:已取消;10:待付款;30:待使用;40:交易完成(已使用)
            String orderStatus="";
            if("0".equals(details.getOrderState())){
                orderStatus="已取消";
            }else if("10".equals(details.getOrderState())){
                orderStatus="待付款";
            }else if("30".equals(details.getOrderState())){
                orderStatus="待使用";
            }else if("40".equals(details.getOrderState())){
                orderStatus="已使用";
            }
            tvCourseState.setText(orderStatus);
        }else{
            tvCourseState.setText("");
        }


        GlideHelper.showImage(context,details.getGoodsImage(),ivShopping);
        tvPrice.setText("￥"+details.getGoodsPrice());
        tvAmount.setText("x "+details.getGoodsNum());
        //服务密码，暂时不知道干啥用
//        if(details.getServicePwd()!=null&&details.getServicePwd().size()>0){
//            tvTel.setText(details.getServicePwd().get(0)+"");
//        }
//        tvTel.setText(details.getServicePwds());
        tvMerchantContent.setText(details.getAddress());
        tTvServeTime.setText(details.getServiceTime());
        if(!AbStrUtil.isEmpty(details.getRemark())){
            tvLeaveMessage.setText(details.getRemark());
        }
        tvTotalPrice.setText("￥"+details.getOrderTotalPrice());
        tvCoupon.setText("-￥"+details.getCouponPrice());
        tvPayment.setText("￥"+details.getOrderAmount());
        if(!AbStrUtil.isEmpty(details.getPaymentTime())){
            tvPaymentConfirmTime.setText("付款时间："+details.getPaymentTime());
        }else{
            tvPaymentConfirmTime.setVisibility(View.GONE);
        }
        tvOrderNo.setText("订单编号："+details.getOrderSn());
    }

    @Override
    protected void setData() {
        getData();
    }


    private class ServicePasswordAdapter extends RecyclerView.Adapter<ServicePasswordAdapter.ViewHolder>{
        private List<String> data=new ArrayList<>();
        public void addAll(List<String> data){
            this.data.addAll(data);
            this.notifyDataSetChanged();
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(context).inflate(R.layout.activity_cosmetic_training_course_detail_password_item,parent,false);
            ViewHolder viewHolder=new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.tvTel.setText(data.get(position));
            holder.tvReimburse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    intent.setClass(context,AppointmentSaleActivity.class);
                    intent.putExtra("orderId",orderId);
                    intent.putExtra("servicePassword",data.get(position));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            TextView tvReimburse,tvTel;
            public ViewHolder(View itemView) {
                super(itemView);
                tvTel=ButterKnife.findById(itemView,R.id.training_course_tv_tel);
                tvReimburse=ButterKnife.findById(itemView,R.id.training_course_tv_reimburse);
            }
        }
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}
